package com.tanhua.server.controller;

import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private UserService userService;


    /** 发送手机验证码
     * 1.请求路径：/user/login
     * 2.请求参数：phone(手机号码)用map集合接收，因为是JSON形式，用@RequestBody注解转换为java对象
     * 3.返回结果：无返回
     * @param map
     */
    @PostMapping("/login")
    public void login(@RequestBody Map map){
        //1.接收参数
        String  phone = (String) map.get("phone");

        //2.调用service
        String randomCode = userService.sendMsm(phone);

        System.out.println("【探花交友】验证码："+randomCode+",验证码五分钟有效，请勿泄露");
    }




    /**
     * 用户登录
     * 1.请求路径：/user/loginVerification
     * 2.请求参数：phone(String),verificationCode(String)用map集合封装
     * 3.返回结果：token，isNew(封装到map集合返回)
     */
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map map){
            //1.接收参数
            String phone = (String) map.get("phone");
            String code = (String) map.get("verificationCode");

            //2.调用userServi
            //返回结果用map封装
            Map result = userService.loginVerification(phone, code);

            return ResponseEntity.ok(result);



    }
}
